//=====================================================================
//    COPYRIGHT(C) Innobeam
//    ALL RIGHTS RESERVED
//=====================================================================
//Filename    : CAN_send.v rev 1.0
//Created On  : 2019-10-11
//Author      : fei.yu
//*Description:
	//*1.发送拼接规律： 标识+地址+48位数据
	//*2.OA是主控发到各个控制器的标志；OB是各个控制器到主控的标志
//Include     :             
//Modification: fei.yu
//=====================================================================
module CAN_send(	iClk 			,
					iRst_n			,
					//to lan9252 module
					
					//to data_operation & pack module
					ivBSC_LED_1,			
					ivBSC_LED_2,		
					ivHHC_Gantry_1,
					ivHHC_Collimator_1,			
					ivHHC_Swing_1,			
					ivHHC_MLCX1_1,				
					ivHHC_MLCX2_1,				
					ivHHC_JawY1_1,					
					ivHHC_JawY2_1,				
					ivHHC_CouchVRT_1,			
					ivHHC_CouchLAT_1,	
					ivHHC_CouchLNG_1,
					ivHHC_CouchLED1_1,
					ivHHC_CouchLED2_1,
					ivHHC_DisplayState_1,
					ivHHC_CouchLNGPLAN_1,
					ivHHC_MLCX_1,
					ivHHC_MLCY_1,
					ivHHC_Gantry_2,
					ivHHC_Collimator_2,			
					ivHHC_Swing_2,					
					ivHHC_MLCX1_2,				
					ivHHC_MLCX2_2,				
					ivHHC_JawY1_2,					
					ivHHC_JawY2_2,				
					ivHHC_CouchVRT_2,			
					ivHHC_CouchLAT_2,	
					ivHHC_CouchLNG_2,
					ivHHC_CouchLED1_2,
					ivHHC_CouchLED2_2,
					ivHHC_DisplayState_2,
					ivHHC_CouchLNGPLAN_2,
					ivHHC_MLCX_2,
					ivHHC_MLCY_2,
                    ovCanData_send1,
                    ovCanData_send2,
                    ovCanData_send3,
                    ovCanData_send4
					);
//========================================================================
//    parameter
//========================================================================

parameter	DLY_TIME = 32'd200_000;

//========================END=============================================


//========================================================================
//    port
//========================================================================
input 					iClk;
input 					iRst_n;


input		[15:0]		ivBSC_LED_1;		
input		[15:0]		ivBSC_LED_2;	
input		[15:0]		ivHHC_Gantry_1;
input		[15:0]		ivHHC_Collimator_1;		
input		[15:0]		ivHHC_Swing_1;		
input		[15:0]		ivHHC_MLCX1_1;		
input		[15:0]		ivHHC_MLCX2_1;		
input		[15:0]		ivHHC_JawY1_1;		
input		[15:0]		ivHHC_JawY2_1;	
input		[15:0]		ivHHC_CouchVRT_1;
input		[15:0]		ivHHC_CouchLAT_1;	
input		[15:0]		ivHHC_CouchLNG_1;
input		[15:0]		ivHHC_CouchLED1_1;
input		[15:0]		ivHHC_CouchLED2_1;
input		[15:0]		ivHHC_DisplayState_1;
input		[15:0]		ivHHC_CouchLNGPLAN_1;
input		[15:0]		ivHHC_MLCX_1;	
input		[15:0]		ivHHC_MLCY_1;	
input		[15:0]		ivHHC_Gantry_2;
input		[15:0]		ivHHC_Collimator_2;		
input		[15:0]		ivHHC_Swing_2;		
input		[15:0]		ivHHC_MLCX1_2;		
input		[15:0]		ivHHC_MLCX2_2;		
input		[15:0]		ivHHC_JawY1_2;		
input		[15:0]		ivHHC_JawY2_2;	
input		[15:0]		ivHHC_CouchVRT_2;
input		[15:0]		ivHHC_CouchLAT_2;	
input		[15:0]		ivHHC_CouchLNG_2;
input		[15:0]		ivHHC_CouchLED1_2;
input		[15:0]		ivHHC_CouchLED2_2;
input		[15:0]		ivHHC_DisplayState_2;
input		[15:0]		ivHHC_CouchLNGPLAN_2;
input		[15:0]		ivHHC_MLCX_2;	
input		[15:0]		ivHHC_MLCY_2;

output      [63:0]       ovCanData_send1;
output      [63:0]       ovCanData_send2;
output      [63:0]       ovCanData_send3;
output      [63:0]       ovCanData_send4;
//========================END=============================================


//========================================================================
//*Unpack
//========================================================================
reg      [63:0]       rvCanData_send1;
reg      [63:0]       rvCanData_send2;
reg      [63:0]       rvCanData_send3;
reg      [63:0]       rvCanData_send4;
reg      [ 12:0]       rvSendCounter1,rvSendCounter2,rvSendCounter3,rvSendCounter4;

assign ovCanData_send1 = rvCanData_send1;
assign ovCanData_send2 = rvCanData_send2;
assign ovCanData_send3 = rvCanData_send3;
assign ovCanData_send4 = rvCanData_send4;

//BSC1的数据发送(0A)
always@(posedge iClk or negedge iRst_n)begin
	if(!iRst_n)begin
		rvCanData_send1 <= 64'd0;
		rvSendCounter1  <= 13'd0;
		end
	else begin
		rvSendCounter1<=(rvSendCounter1>=13'd3000)?13'd0:rvSendCounter1+1;
		case (rvSendCounter1)
        1000:begin rvCanData_send1<={8'h0A,8'd1,32'd0,ivBSC_LED_1}; end
        2000:begin rvCanData_send1<={8'h0A,8'd2,32'd0,ivBSC_LED_1}; end 
		3000:rvCanData_send1<=rvCanData_send1;
        default:begin rvCanData_send1<=rvCanData_send1;  end
        endcase
		end
	end		

//BSC2的数据发送(0B)
always@(posedge iClk or negedge iRst_n)begin
	if(!iRst_n)begin
		rvCanData_send2 <= 64'd0;
		rvSendCounter2  <= 13'd0;
		end
	else begin
		rvSendCounter2<=(rvSendCounter2>=13'd3000)?13'd0:rvSendCounter2+1;
		case (rvSendCounter2)
        1000:begin rvCanData_send2<={8'h0A,8'd1,32'd0,ivBSC_LED_2}; end
        2000:begin rvCanData_send2<={8'h0A,8'd2,32'd0,ivBSC_LED_2}; end 
		3000:begin rvCanData_send2<=rvCanData_send2; end
        default:begin rvCanData_send2<=rvCanData_send2;  end
        endcase
		end
	end	

//HHC1的数据发送(0C)
always@(posedge iClk or negedge iRst_n)begin
	if(!iRst_n)begin
		rvCanData_send3 <= 64'd0;
		rvSendCounter3  <= 13'd0;
		end
	else begin
		rvSendCounter3<=(rvSendCounter3>=13'd7000)?13'd0:rvSendCounter3+1;
		case (rvSendCounter3)
        1000:begin  rvCanData_send3<={8'h0A,8'd1,ivHHC_Swing_1,ivHHC_Collimator_1,ivHHC_Gantry_1}; end
        2000:begin  rvCanData_send3<={8'h0A,8'd2,ivHHC_JawY1_1,ivHHC_MLCX2_1,ivHHC_MLCX1_1}; end
        3000:begin  rvCanData_send3<={8'h0A,8'd3,ivHHC_CouchLAT_1,ivHHC_CouchVRT_1,ivHHC_JawY2_1};end
        4000:begin  rvCanData_send3<={8'h0A,8'd4,ivHHC_CouchLED2_1,ivHHC_CouchLED1_1,ivHHC_CouchLNG_1}; end
        5000:begin  rvCanData_send3<={8'h0A,8'd5,ivHHC_MLCX_1,ivHHC_CouchLNGPLAN_1,ivHHC_DisplayState_1}; end
        6000:begin rvCanData_send3<={8'h0A,8'd6,32'd0,ivHHC_MLCY_1}; end
		7000:begin rvCanData_send3<=rvCanData_send3; end
        default:begin rvCanData_send3<=rvCanData_send3;  end
        endcase
		end
	end

//HHC2的数据发送(0D)
always@(posedge iClk or negedge iRst_n)begin
	if(!iRst_n)begin
		rvCanData_send4 <= 64'd0;
		rvSendCounter4  <= 13'd0;
		end
	else begin
		rvSendCounter4<=(rvSendCounter4>=13'd7000)?13'd0:rvSendCounter4+1;
		case (rvSendCounter4)
        1000:begin  rvCanData_send4<={8'h0A,8'd1,ivHHC_Swing_2,ivHHC_Collimator_2,ivHHC_Gantry_2}; end
        2000:begin  rvCanData_send4<={8'h0A,8'd2,ivHHC_JawY1_2,ivHHC_MLCX2_2,ivHHC_MLCX1_2}; end
        3000:begin  rvCanData_send4<={8'h0A,8'd3,ivHHC_CouchLAT_2,ivHHC_CouchVRT_2,ivHHC_JawY2_2}; end
        4000:begin  rvCanData_send4<={8'h0A,8'd4,ivHHC_CouchLED2_2,ivHHC_CouchLED1_2,ivHHC_CouchLNG_2}; end
        5000:begin  rvCanData_send4<={8'h0A,8'd5,ivHHC_MLCX_2,ivHHC_CouchLNGPLAN_2,ivHHC_DisplayState_2}; end
        6000:begin rvCanData_send4<={8'h0A,8'd6,32'd0,ivHHC_MLCY_2}; end
		7000:begin rvCanData_send4<=rvCanData_send4; end
        default:begin rvCanData_send4<=rvCanData_send4;  end
        endcase
		end
	end
endmodule


